# 导包
import urllib3

from app import BASE_DIR


# 没有继承,可以不用加括号
class LoginApi:
    # 初始化
    def __init__(self):
        # 1.忽略警告--去除很多报错
        urllib3.disable_warnings()
        # 验证码url
        self.url_verify="https://demo6.tp-shop.cn/index.php?m=Home&c=User&a=verify"
        # 登录url
        self.url_login= "https://demo6.tp-shop.cn/index.php?m=Home&c=User&a=do_login"


    # 获取验证码
    def get_verify_code(self,session):
        response=session.get(self.url_verify, verify=False)
        with open(BASE_DIR+"/data/code.png", "wb") as f:
            f.write(response.content)
        code = input('请输入验证码吧：')

        return response,code


    # 登录
    def login(self,session,username,password,verify_code):
        login_data={
            'username': username,
            'password': password,
            'verify_code':verify_code
        }
        return session.post(url=self.url_login,data=login_data, verify=False)